
package com.jims.master.modules.doctor.netHosptial.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.modules.base.user.entity.MasterBaseinfo;
import com.jims.master.modules.base.user.entity.MasterContactinfo;
import com.jims.master.modules.base.user.entity.MasterDoctorinfo;
import com.jims.master.modules.base.user.service.MasterBaseinfoService;
import com.jims.master.modules.base.user.service.MasterContactinfoService;
import com.jims.master.modules.base.user.service.MasterDoctorinfoService;
import com.jims.master.modules.doctor.netHosptial.dao.NetClinicDao;
import com.jims.master.modules.doctor.netHosptial.entity.NetClinic;
import com.jims.master.modules.doctor.netHosptial.entity.NetPatMaster;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 义诊服务/互联网医院医患关联表Service
 * @author xueyx
 * @version 2016-08-03
 */
@Service
@Transactional(readOnly = true)
public class NetClinicService extends CrudService<NetClinicDao, NetClinic> {

	/*@Autowired
	private MasterDoctorinfoService masterDoctorinfoService;

	@Autowired
	private NetPatMasterService netPatMasterService;*/

	@Override
    public NetClinic get(String id) {
		return super.get(id);
	}
	
	@Override
    public List<NetClinic> findList(NetClinic NetClinic) {
		return super.findList(NetClinic);
	}

	public List<NetClinic> findAllList(NetClinic NetClinic) {
		return dao.findAllList(NetClinic);
	}
	
	@Override
    public Page<NetClinic> findPage(Page<NetClinic> page, NetClinic NetClinic) {
		return super.findPage(page, NetClinic);
	}
	
	@Override
    @Transactional(readOnly = false)
	public int save(NetClinic NetClinic) {
		return super.save(NetClinic);
	}
	
	@Override
    @Transactional(readOnly = false)
	public int delete(NetClinic NetClinic) {
		return super.delete(NetClinic);
	}

	//按医生ID和患者beseinfo查询
	public List<NetClinic> findListByEntity(NetClinic NetClinic){
		return dao.findListByEntity(NetClinic);
	};

	@Transactional(readOnly = false)
	public String savefree(MasterBaseinfo baseInfo,MasterContactinfo contactInfo) {
		String result="";
		/*baseInfo.setId(null);
		//contactInfo.setBaseinfo(baseInfo);
		List<MasterContactinfo>  contactInfoList=null;
		//CDAndPhone查询用户是否存在
		//contactInfoList = contactInfoService.findListByCDAndPhone(contactInfo);
		NetClinic NetClinic = null;
		MasterBaseinfo doctorbase = UserUtils.getUser().getMasterBaseinfo();
		Doctor doctor = masterDoctorinfoService.getDocByBaseinfo(doctorbase);
		List<NetClinic>  NetClinicList =null;
		//用户是否存在
		if(contactInfo!=null&& StringUtils.isNotBlank(contactInfo.getId())){
			NetClinic = new NetClinic();
			NetClinic.setDoctorid(doctor.getId());
			NetClinic.setBaseinfo(contactInfo.getBaseinfo().getId());
			if(baseInfo.getFreetype().equals("1")){
				NetClinic.setFreetype("1");
			}else{
				NetClinic.setFreetype("0");
			}
			//关联数据是否存在
			NetClinicList= findListByEntity(NetClinic);
			if(NetClinicList!=null&&NetClinicList.size()>0){
				result = "linked";
			}else{
				save(NetClinic);
				result = "exit";
			}
		}else{
			//新建用户
			masterBaseinfoService.saveAll(baseInfo,contactInfo);
			NetClinic = new NetClinic();
			NetClinic.setDoctorid(doctor.getId());
			contactInfoList = contactInfoService.findListByCDAndPhone(contactInfo);
			if(NetClinic!=null){
				NetClinic.setBaseinfo(contactInfoList.get(0).getBaseinfo().getId());
				save(NetClinic);
				result= "success";
			}else{
				result= "error";
			}
		}
		NetPatMaster netPatMaster = new NetPatMaster();
		netPatMaster.setIdNo(baseInfo.getIdCard());
		netPatMasterService.initialIndexInfoByIdNo(netPatMaster);*/
		return result;
	}
	//虚拟诊室不强制要求就诊时录入身份证
	//但作为信息系统只有有唯一标识身份证，系统中数据才能统一起来
	//如果无身份证则不在系统中创建用户，只新建患者信息，用patientid关联就诊，但平台系统无此患者用户
	/*@Transactional(readOnly = false)
	public String saveVillageFree(MasterBaseinfo baseInfo,MasterContactinfo contactInfo,String orgId) {
		//方便后边一个contactInfo参数，调用baseInfo和contactInfo的值主要是身份证和电话
		*//*contactInfo.setBaseinfo(baseInfo);
		baseInfo.setId(null);*//*
		//查找医生ID
		MasterBaseinfo doctorbase = UserUtils.getCurrentBaseInfo();
		MasterDoctorinfo doctor = masterDoctorinfoService.getDocByBaseinfo(doctorbase);
		//有身份证信息
		*//*if(StringUtils.isNotEmpty(baseInfo.getIdCard())){
			//平台不存在用户
			if(!(contactInfo!=null&& StringUtils.isNotBlank(contactInfo.getId()))){
				masterBaseinfoService.saveAll(baseInfo,contactInfo);
			}else{
				//baseInfo,contactInfo总是一起生成的，虚拟诊室加载数据是contactInfo.id
				//加载数据是电话、身份证都可以，但不能保证二者数据原来都有，这里重新保存一下
				contactInfoService.save(contactInfo);
				baseInfo.setId(contactInfoService.get(contactInfo).getBaseinfo().getId());
				masterBaseinfoService.save(baseInfo);
			}
		}*//*
		NetPatMaster netPatMaster = new NetPatMaster();
		netPatMaster.setIdNo(baseInfo.getIdCard());
		netPatMaster.setName(baseInfo.getName());
		if(StringUtils.isNotEmpty(baseInfo.getAge()))
		netPatMaster.setAge(baseInfo.getAge());
		netPatMaster.setSex(baseInfo.getSex());
		netPatMaster.setDateOfBirth(baseInfo.getBirthDate());
		netPatMaster.setPhoneNumberBusiness(contactInfo.getMobilePhone());
		netPatMaster.setMailingAddress(baseInfo.getAreaDetail());
//		netPatMaster =netPatMasterService.initialIndexInfoByIdNo(netPatMaster);
		//查找患者
		List<MasterContactinfo> contactInfoList = null;
		//有身份证信息
		//医师患者关系是否存在
		*//*if(StringUtils.isNotEmpty(baseInfo.getIdCard())){
			contactInfoList = contactInfoService.findListByCDAndPhone(contactInfo);
			//机构患者是否存在
			//不需要了，机构患者关系可以用挂号表取得，patientId/挂号科室 ，patientId/上级医院的hisServerIP、orgId
			//医师患者关系是否存在
			initialDoctorFreeClinic(baseInfo.getFreetype(), contactInfoList.get(0), doctor, netPatMaster.getPatientId(),orgId);
		}else{
			//无身份证，无平台用户，和平台的机构无关联
			//医师患者关系是否存在
			initialDoctorFreeClinic(baseInfo.getFreetype(), null, doctor,netPatMaster.getPatientId(),orgId);
		}*//*
		return netPatMaster.getPatientId();
	}*/
	//医师患者关系是否存在
	private void initialDoctorFreeClinic(String freetype,MasterContactinfo contactInfo,MasterDoctorinfo doctor,String patientId,String orgId){
		NetClinic netClinic = new NetClinic();
		netClinic.setDoctorid(doctor.getId());
		netClinic.setOrgId(orgId);
		netClinic.setPatientId(patientId);
		//设置虚拟诊室类型
		if(freetype.equals("1")){
			netClinic.setFreetype("1");
		}else{
			netClinic.setFreetype("0");
		}
		if(contactInfo!=null){
			//有身份证，有平台用户关联
			netClinic.setBaseinfo(contactInfo.getBaseinfo().getId());
			//关联数据是否存在
			List<NetClinic> netClinicList= findListByEntity(netClinic);
			if(netClinicList!=null&&netClinicList.size()>0) {
				//存在更新，变更更新时间，用来显示最新看病患者
				if (netClinicList.get(0).getIsNewRecord()){
					netClinicList.get(0).preInsert();
					dao.insert(netClinicList.get(0));
				}else{
					netClinicList.get(0).preUpdate();
					dao.update(netClinicList.get(0));
				}
			}else{
				//否则新建
				if (netClinic.getIsNewRecord()){
					netClinic.preInsert();
					dao.insert(netClinic);
				}else{
					netClinic.preUpdate();
					dao.update(netClinic);
				}
			}
		}else{
			//无平台用户
			if (netClinic.getIsNewRecord()){
				netClinic.preInsert();
				dao.insert(netClinic);
			}else{
				netClinic.preUpdate();
				dao.update(netClinic);
			}
		}
	}

	//查询卫生院医生就医的患者及患者的数据传输状态
	public List<NetClinic> findVersionList(NetClinic netClinic){
		return dao.findVersionList(netClinic);
	}
	/**
	 * 医生添加患者基本信息
	 * @param netClinic 医患关系
	 * @return int
	 * @author WangYQ
	 */
	public int relativity(NetClinic netClinic){return dao.relativity(netClinic);};
}